<template>
    <div class="default-main ba-table-box">


        <div style="background: #fff; padding: 13px 15px; padding-bottom: 0;">
            <span :class="[state.customer_id == 0 ? 'btn btn-info mr-12 mb-10' : 'btn mr-12 mb-10']" @click="changeCustomer(0)">{{ t('全部') }}</span>

            <span v-for="(item, index) in state.customer" :key="index" :class="[state.customer_id == item.id ? 'btn btn-info mr-12 mb-10' : 'btn mr-12 mb-10']" @click="changeCustomer(item.id)">
                {{ item.corp_name_ko }}
            </span>

        </div>

        <div style="background: #fff; padding: 13px 15px; padding-top: 0; padding-bottom: 0;" v-if="state.customer_id > 0">
            <span :class="[state.category_id == 0 ? 'btn btn-info mr-12 mb-10' : 'btn mr-12 mb-10']" @click="changeCategory(0)">{{ t('全部') }}</span>
            <span v-for="(item, index) in state.category" :key="index">
                <span v-if="item.customer_id == state.customer_id" :class="[state.category_id == item.id ? 'btn btn-info mr-12 mb-10' : 'btn mr-12 mb-10']" @click="changeCategory(item.id)">
                    {{ item.name_cn }}
                </span>
            </span>
            

        </div>


        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['add', 'quickSearch', 'delete']" :quick-search-placeholder="t('搜索')">
            
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">
            <template #category>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('所属分类')" align="center" width="120">
                    <template #default="scope">
                        <div class="hover" @click="clickCategory(scope.row.category_id)">{{ scope.row.name_cn }}</div>
                    </template>
                </el-table-column>
            </template>
            <template #customer_supplier>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('客户名')" align="center" width="120">
                    <template #default="scope">
                        <div >{{ scope.row.customer_name_ko }}</div>
                    </template>
                </el-table-column>
            </template>
            <template #code>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('产品编码')" align="center" width="120">
                    <template #default="scope">
                        <div class="ellipsis-1">{{ scope.row.code }}</div>
                    </template>
                </el-table-column>
            </template>
            <template #name_specification>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('产品名称')" align="center" minWidth="200">
                    <template #default="scope">
                        <div >{{ scope.row.name_customer }}</div>
                        <div>{{ scope.row.name_supplier }}</div>
                    </template>
                </el-table-column>
            </template>

           
            
            <template #buy>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('购买价格')" align="center" width="100">
                    <template #default="scope">
                        <div>{{ number_format(scope.row.buy_price) }} {{ scope.row.buy_currency }}</div>
                    </template>
                </el-table-column>
            </template>
            <template #sale>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('销售价格')" align="center" width="100">
                    <template #default="scope">
                        <div>{{ number_format(scope.row.sale_price) }} {{ scope.row.sale_currency }}</div>
                    </template>
                </el-table-column>
            </template>

            <template #drawing>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('图纸')" align="center">
                    <template #default="scope">
                        <div>
                            <div v-for="(item, index) in scope.row.drawing" :key="index">
                                <el-link :href="item" target="_blank">{{ t('查看') }}</el-link>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            
        </Table>

        <!-- 表单 -->
        <PopupForm />

        

    </div>
    
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import { getCustomerApi } from '/@/api/most/product/product'
import { number_format } from '/@/utils/common'



defineOptions({
    name: 'system/translate/index',
})

const { t } = useI18n()
const tableRef = ref()


const drawer = ref(false)

const state = reactive({
    update_time: '',
    direction: ref('ltr'),
    data: {
        'q_cn': '',
        'h_cn': '',
        'q_ko': '',
        'h_ko': '',
        'q_en': '',
        'h_en': ''
    },
    customer: {},
    category: {},
    customer_id: 0,
    category_id: 0
});


const changeCustomer = (customer_id: number) => {
    state.customer_id = customer_id;

    baTable.table.filter!.customer_id = customer_id;
    baTable.table.filter!.category_id = 0;
    baTable.table.filter!.supplier_id = 0;

    baTable.table.filter!.page = 1

    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}

const changeCategory = (category_id: number) => {
    state.category_id = category_id;

    baTable.table.filter!.category_id = category_id;
    baTable.table.filter!.supplier_id = 0;

    baTable.table.filter!.page = 1

    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}

const clickCategory = (category_id: number) => {
    baTable.table.filter!.category_id = category_id;
    baTable.table.filter!.page = 1

    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}

const clickSupplier = (supplier_id: number) => {
    baTable.table.filter!.supplier_id = supplier_id;
    baTable.table.filter!.page = 1

    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}


let optButtons = defaultOptButtons([])


// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: t('编辑'),
        type: 'primary',
        class: 'btn btn-primary btn-small',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: t('删除'),
        type: 'danger',
        class: 'btn btn-danger mt-0 btn-small ml-0',
        icon: '',
        popconfirm: {
            confirmButtonText: t('删除'),
            cancelButtonText: t('取消'),
            confirmButtonType: 'danger',
            title: t('确定要删除这条记录吗') + '?',
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)




/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/product.Product/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('客户名'), prop: 'customer_name_ko', align: 'center', render: 'slot', slotName: 'customer_supplier' },
            { label: t('供货商'), prop: 'corp_name_cn', align: 'center', width: 250, render: 'slot', slotName: 'supplier_name' },
            { label: t('所属分类'), prop: 'hs_code_id', align: 'center', width: 120, render: 'slot', slotName: 'category' },
            { label: t('产品编码'), prop: 'code', align: 'center', width: 120, render: 'slot', slotName: 'code' },
            { label: t('产品名称'), prop: 'name_customer', align: 'center', width: 220, render: 'slot', slotName: 'name_specification' },
            // { label: t('规格'), prop: 'specification_supplier', align: 'center', width: 180 },
            { label: t('图纸'), prop: 'drawing_image', align: 'center', width: 150, render: 'images' },
            
            // { label: t('购买价格'), prop: 'id', align: 'center', render: 'slot', slotName: 'buy' },
            // { label: t('销售价格'), prop: 'id', align: 'center', render: 'slot', slotName: 'sale' },
            { label: t('操作'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 150 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { neixiao: false },
    },{
        
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后
            state.update_time = res.data.update_time;
            // console.log(res.data);
            // console.log(state.sub_status)
        },
        requestEdit: ({ res }: {res: ApiResponse}) => { // 编辑请求后
            
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })

    getCustomerApi({}).then((e) => {
        state.customer = e.data.customer;
        state.category = e.data.category;
    })
})
</script>

<style scoped lang="scss">


:deep(.el-image){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    // max-width: 80px;
    // max-height: 80px;
}
:deep(td.business-name > div.cell) {
    min-height: 80px;
    display:flex;
    justify-content:center; 
    align-items:center;
}

:deep(.el-image > img){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    max-width: 80px;
    max-height: 30px;
}



:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
:deep(.pay-type .el-radio){
    margin-right: 10px;
}
pre{
    font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;
}

:deep(.ml-0){
    margin-left: 0!important;
}
:deep(.mt-8){
    margin-top: 8px;
}
:deep(.ml--6){
    margin-left: -6px;
}
:deep(.ml-5){
    margin-left: 5px;
}
:deep(.mt-5){
    margin-top: 5px;
}
:deep(.mr-12){
    margin-right: 12px;
}
:deep(.mb-10){
    margin-bottom: 12px;
}

.hover{
    cursor: pointer;
}
.hover:hover{
    color: #409EFF;
}

:deep(.ba-operate-dialog-product-edit .el-dialog__body) {
    height: 430px;
    max-height: 100%;
}

.ellipsis-1 {
    white-space: nowrap;      /* 强制文本在一行内显示 */
    overflow: hidden;        /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 超出部分显示省略号 */
}

</style>
